using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;

namespace Webbness.VsTool
{
    [Serializable]
    internal class LocalDocumentTypeResolver : XmlUrlResolver
    {
        protected string systemEntry;

        public LocalDocumentTypeResolver(string systemEntry)
        {
            this.systemEntry = systemEntry;
        }

        public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
        {
            Match m = new Regex(@"(.)*\.dtd$").Match(absoluteUri.AbsolutePath);
            if (m.Success)
            {
                return new FileStream(this.systemEntry, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            return base.GetEntity(absoluteUri, role, ofObjectToReturn);
        }
    }
}